package chess.client;

import com.allen_sauer.gwt.dnd.client.DragContext;
import com.allen_sauer.gwt.dnd.client.PickupDragController;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.RootPanel;

public class ChessBoardDragController extends PickupDragController {
	public ChessBoard chessBoard;
	
	public ChessBoardDragController(ChessBoard chessBoard, AbsolutePanel boundaryPanel,
			boolean allowDroppingOnBoundaryPanel) {
		super(boundaryPanel, allowDroppingOnBoundaryPanel);
		
		this.chessBoard = chessBoard;
	}

	@Override	
	public void dragStart() {		
		DragContext context = super.context;
		
		int[] aSquarePos = getChessSquare(context);
		
		chessBoard.activeChessPiece = chessBoard.chessSquares[aSquarePos[0]][aSquarePos[1]];
		
		super.dragStart();
	}

	
	public static int[] getChessSquare(DragContext context)
	{
		int nRow = 7-((context.mouseY - RootPanel.get("ChessContainer").getAbsoluteTop()) / Constants.ChessSquareSize);
		int nCol = (context.mouseX - RootPanel.get("ChessContainer").getAbsoluteLeft()) / Constants.ChessSquareSize;
				
		return new int[] {nRow, nCol};
	}
}
